home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0971.dms / q0971.adf / programmer / prog / proc_main.c < prev    next >
C/C++ Source or Header  |  1997-04-10  |  11KB  |  391 lines

  1. /****************************************************************/
  2. /*  File proc_main.c                        */
  3. /*                                */
  4. /*  For superdark project                    */
  5. /*                                */
  6. /* This file have to be linked with evry superdark module. The  */
  7. /* main function make various initialisation, test if superdark */
  8. /* is here, wait for a signal from superdark and then call the    */
  9. /* dark() function. Read the programmers.doc for more  info.    */
  10. /*                                */
  11. /*    @Thomas Landspurg 93                    */
  12. /*                                */
  13. /****************************************************************/
  14. /*  17/06/93 : v2.0  CPU timeout information added        */
  15. /*  20/06/93 : v2.1  SDwait() function added, and SDprintf()    */
  16. /*                   A 'could be a bug later' removed: sd watch.*/
  17. /*             can be stopped if the effect was not start.*/
  18. /*             before                    */
  19. /*                                */
  20. /****************************************************************/
  21.  
  22. #include    <stdio.h>
  23. #include     <stdlib.h>
  24. #include    <exec/types.h>
  25. #include    <exec/tasks.h>
  26. #include    <exec/ports.h>
  27. #include    <exec/memory.h>
  28. #include    <graphics/displayinfo.h>
  29. #include    <exec/tasks.h>
  30. #include    <dos/dosextens.h>
  31.  
  32. #include    <proto/exec.h>
  33. #include    <proto/dos.h>
  34. #include    <proto/graphics.h>
  35. #include    <proto/intuition.h>
  36.  
  37. #include    "/includes/proc_main_protos.h"
  38. #include    "/includes/struct.h"
  39. #include    "/includes/tom_gadget.h"
  40.  
  41. #define    VERSION    2
  42.  
  43. WORD    flg_ctrl_c;
  44. struct    my_data    *p_info;        /* Private use                  */
  45. struct    appel_proc    *p_data_proc;    /* Communication structure with SDark */
  46. struct    Task    *my_t;
  47.  
  48.  
  49. struct    MsgPort        *MyPort=NULL;
  50. struct    BlankerMsg    *MyMsg=NULL;
  51.  
  52. /***********************************************************/
  53. /* These information SHOULD be give by the blanking module */
  54. /***********************************************************/
  55.  
  56. extern    struct    tom_gadget    my_gadg[]; /* Array of gadget for window configuration */
  57. extern    char    *p_text_info;        /* The text information, show in the    */
  58.                     /* info window                */
  59. void    MyWrite(char    *pc);
  60.  
  61. UWORD    __chip null_pointer[]={0,0,0,0,0,0};
  62. BPTR    hd=0;
  63. char    pr_buff[100];
  64.  
  65. /* Note: The folloing SPrintF Routine is taken from the program "FracBlank " */
  66. /* by:                                         */UWORD PutChar[2] = {0x16C0,0x4E75};
  67.  
  68. /* dirty hack to avoid assembler part :-)
  69.    16C0: move.b d0,(a3)+
  70.    4E75: rts
  71. */
  72. struct    MsgPort    *mp;
  73.  
  74. #define VOID_STDARGS   void __stdargs
  75.  
  76. /************************************************************************/
  77. /* This exactly the same function than printf, but printf() can't be    */
  78. /* used in a blanker. Use the utility program show_info to see the result */
  79. /* NOTE: Always use long with number, like %ld,%lx and NOT %x or %d    */
  80. /************************************************************************/
  81.  
  82. VOID_STDARGS SDprintf(char *FormatString,...)
  83.  
  84. {
  85.     char    buffer[100];
  86.     if(hd){
  87.         RawDoFmt (FormatString,(APTR)((LONG *)&FormatString+1L),
  88.                (void *)PutChar,buffer);
  89.         MyWrite(buffer);
  90.     }
  91. }
  92.  
  93. /************************************************************************/
  94. /* Some variouse function used for debugging..use them at your own risk    */
  95. /* but do not use any printf function                    */
  96. /************************************************************************/
  97.  
  98. VOID_STDARGS SPrintF(char *pr_buff,char *FormatString,...)
  99.  
  100. {
  101.  RawDoFmt (FormatString,(APTR)((LONG *)&FormatString+1L),
  102.            (void *)PutChar,pr_buff);
  103. }
  104.  
  105. /************************************************************************/
  106.  
  107. mystrlen(char    *pc)
  108. {
  109.     int    ret;
  110.     ret=0;
  111.     while(*pc++!=0){
  112.         ret++;
  113.     }
  114.     return    ret;
  115. }
  116.  
  117. /************************************************************************/
  118.  
  119. void    MyWrite(char    *pc)
  120. {
  121.     if(hd!=0)Write(hd,pc,mystrlen(pc));
  122. }
  123. void    My_Print(struct RastPort *rp,UWORD x,UWORD y,char    *pc)
  124. {
  125.     Move(rp,x,y);
  126.     Text(rp,pc,mystrlen(pc));
  127. }
  128.  
  129.  
  130. /*******************************************************************/
  131. /* This function is used instead of CloseScreen, because if we are */
  132. /* in the random mode, we don't close immediatly the screen, but   */
  133. /* we give it back to superdark, so he can use it to create next   */
  134. /* screen                               */
  135. /*******************************************************************/
  136.  
  137. void    DCloseScreen(struct Screen *s)
  138. {
  139.     struct  Screen *st;
  140.  
  141.     if(mp!=0){
  142.         st=p_data_proc->scr;
  143.     }else{
  144.         st=NULL;
  145.     }
  146.     if(st!=NULL){
  147.         while(st->FirstWindow)CloseWindow(st->FirstWindow);
  148.         CloseScreen(st);
  149.         p_data_proc->scr=NULL;
  150.     }
  151.  
  152.     if((mp!=NULL)&&(p_info->flg_alea==TRUE)){
  153.         p_data_proc->scr=s;
  154.     }else{
  155.         if(s){
  156.         while(s->FirstWindow)CloseWindow(s->FirstWindow);
  157.         CloseScreen(s);
  158.         if(mp)p_data_proc->scr=NULL;
  159.        }
  160.     }
  161. }
  162.  
  163. /*******************************************************************/
  164. /* this is used to put say to superdark what is the current screen */
  165. /* struct. So, superdark can put it back even if the effect is not */
  166. /* yet finished. This happen when the module have somehting long   */
  167. /* to do (loading image) and the user want to throw the blanker    */
  168. /*******************************************************************/
  169.  
  170. void    DGiveScreen(struct Screen *s)
  171. {
  172.     p_data_proc->current_scr=s;
  173. }
  174. /****************************************************************/
  175.  
  176. void main(void)
  177. {
  178.     struct     Message *MsgExit=NULL;
  179.     LONG    code_ret;
  180.     int        signal;
  181.     BOOL    Flg_Exit;
  182.     ULONG    sigport;
  183.  
  184.    /****************************************************************/
  185.    /* Look for the msg port wich contain informaion about superdark*/
  186.    /****************************************************************/
  187.  
  188.    IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0);
  189.    GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0);
  190.  
  191.    /* If superdark is not here, we start the blanker alone...Be careful! */
  192.    if((mp=FindPort("That's Dark!"))==0){
  193.         dark();
  194.    }else{
  195.     /*********************************************/
  196.     /* Give and get some info to/from SuperDark  */
  197.     /*********************************************/
  198.     p_data_proc=(struct appel_proc *)mp;
  199.     p_info=p_data_proc->p_info;
  200.     hd=p_data_proc->out_hd;
  201.     p_data_proc->p_gadg=my_gadg;
  202.     p_data_proc->p_texte=p_text_info;
  203.     p_data_proc->type_screen=SCR_OWN;/* By default, we create our own screen */
  204.                      /* can be changed in the proc_init fct  */
  205.     p_data_proc->enable_watchdog=TRUE;
  206.                     /* By default, CPU timeout (watchdog) is */
  207.                     /* enabled. Can be chaged in proc_init() */
  208.     p_data_proc->proc_main_ver=VERSION;
  209.                     /* proc_main version...            */
  210.     proc_init();            /* Call the program initialisation    */
  211.  
  212.     /* Note, this will be used by superdark to see if the module    */
  213.     /* is started                            */
  214.     if(p_data_proc->code_ret==-1){
  215.         p_data_proc->code_ret=DARK_WB_13;
  216.     }
  217.  
  218.     my_t=FindTask(0);
  219.     MyWrite("Avant le do\n");
  220.     SPrintF(pr_buff,"Tache mt:%lx\n",my_t);
  221.     MyWrite(pr_buff);
  222.  
  223.          MyPort = CreatePort( 0, 0 );
  224.     MyMsg = AllocMem( sizeof( struct BlankerMsg ), MEMF_PUBLIC|MEMF_CLEAR );
  225.     if(MyPort&&MyMsg){
  226.     
  227.     MyMsg->Msg.mn_ReplyPort = MyPort;
  228.     MyMsg->Msg.mn_Length = sizeof( struct BlankerMsg );
  229.     MyMsg->Blanker_Task  = my_t;
  230.  
  231.     PutMsg( mp, ( struct Message * )MyMsg );
  232.  
  233.     sigport=1<<MyPort->mp_SigBit;    
  234.     Flg_Exit=FALSE;
  235.     do{
  236.  
  237.         signal=Wait(sigport|SIGBREAKF_CTRL_C|SIG_MAIN_DARK_WATCHDOG);
  238.  
  239.         SDprintf("Signal:%lx\n",signal);
  240.         if(signal&sigport){
  241.             while(MyMsg=(struct BlankerMsg *)GetMsg(MyPort)){
  242.                 Type_Action    Action;
  243.                 Action=MyMsg->Action;
  244.                 SDprintf("Message recu,actiob:%ld\n",MyMsg->Action);
  245.             
  246.                 switch(Action){
  247.                     case    DO_DARK:
  248.  
  249.                         ReplyMsg((struct Message *)MyMsg);
  250.                         SDprintf("Signal dark...\n");
  251.                         /* Clear old CTRL_C */
  252.                         SetSignal(0,SIGBREAKF_CTRL_C);
  253.                         flg_ctrl_c=FALSE;
  254.  
  255.                         /* The main call to the dark() function  */
  256.                         code_ret=dark();
  257.  
  258.                         /* Note: this code ret is a new thing since V2.0 */
  259.                         /* So try to put a return code in your dark()     */
  260.                         /* function,0 if OK, Non zero if error         */
  261.                         p_data_proc->code_ret=code_ret;
  262.  
  263.                         SDprintf("Result:%ld\n",flg_ctrl_c);
  264.                         /* If there was an error, wait for the end */
  265.                         if(flg_ctrl_c==FALSE)wait_end();
  266.                         wait_end();
  267.                         MyWrite("ctrl-C receive,dark exited\n");
  268.  
  269.                         /* We have to tell superdark that we have stopped */
  270.                         /* the effect                      */
  271.                         break;
  272.                     case    DO_STOP:
  273.                         ReplyMsg((struct Message *)MyMsg);
  274.                         break;
  275.  
  276.                     case    DO_EXIT:
  277.                         proc_end();
  278.                         Flg_Exit=TRUE;
  279.                         MsgExit=MyMsg;
  280.                         break;
  281.                 }
  282.             }
  283.         }
  284.         if(signal&SIG_MAIN_DARK_WATCHDOG){
  285.             p_data_proc->flg_watchdog--;
  286.             SetSignal(0,SIG_MAIN_DARK_WATCHDOG);
  287.         }
  288.  
  289.     }while(Flg_Exit==FALSE);
  290.     SDprintf("Signal Exit..\n");
  291.     if(MsgExit)ReplyMsg((struct Message *)MsgExit);
  292.     if(MyPort)DeletePort(MyPort);
  293.      }
  294.     }
  295.     CloseLibrary((struct Library *)IntuitionBase);
  296.     CloseLibrary((struct Library *)GfxBase);
  297. }
  298.  
  299. /****************************************/
  300. /* This function put a null pointer in    */
  301. /* in a window                */
  302. /****************************************/
  303.  
  304. void    ClearPtr(struct Window *win)
  305. {
  306.     SetPointer( win, null_pointer, 1, 1, 0, 0);
  307. }
  308.  
  309. /************************************************************/
  310. /* This function add a background window in a given screen, */
  311. /* and add a null pointer in this window.            */
  312. /************************************************************/
  313.  
  314. struct    Window    *AddWindow(struct Screen *s)
  315. {
  316.      struct Window *win;
  317.      win=OpenWindowTags(    NULL,
  318.                 WA_Left,    0,
  319.                 WA_Top,        0,
  320.                 WA_Width,    s->Width,
  321.                 WA_Height,    s->Height,
  322.                 WA_CustomScreen,s,
  323.                 WA_Flags,    WFLG_SIMPLE_REFRESH|WFLG_BORDERLESS|WFLG_ACTIVATE,
  324.                 TAG_DONE);
  325.     if(win){
  326.         ClearPtr(win);
  327.        }
  328.     return(win);
  329. }
  330.  
  331. /**************************************************/
  332. /* This function return TRUE if you have to stop, */
  333. /* FALSE elsewehre.                  */
  334. /**************************************************/
  335.  
  336. int    __saveds tst_end()
  337. {
  338.     ULONG    sig;
  339.  
  340.     
  341.     sig=SetSignal(0,0);
  342.     if(sig&SIGBREAKF_CTRL_C){
  343.         flg_ctrl_c=TRUE;
  344.         return TRUE;
  345.     }else{
  346.         if(sig&SIG_MAIN_DARK_WATCHDOG){
  347.             p_data_proc->flg_watchdog--;
  348.             SetSignal(0,SIG_MAIN_DARK_WATCHDOG);
  349.         }
  350.  
  351.         return FALSE;
  352.     }
  353. }
  354.  
  355. /****************************************************************/
  356. /* Like the exec Wait() function, but take care of the internal */
  357. /* superdark usage. If the return value is exactly 0, this mean */
  358. /* that you have to exit of the blanker                */
  359. /****************************************************************/
  360.  
  361. ULONG    __saveds SDwait(ULONG sigmask)
  362. {
  363.     ULONG    sig;
  364.  
  365.     /* If we have already a ctrl_c,  send a new ctrl_C, to prevent    */
  366.     /* a deadlock if dark() does not use correctly this fct        */
  367.  
  368.     if(flg_ctrl_c){
  369.         Signal(my_t,SIGBREAKF_CTRL_C);
  370.     }
  371.     do{
  372.         sig=Wait(SIGBREAKF_CTRL_C|SIG_MAIN_DARK_DO_DARK|SIG_MAIN_DARK_WATCHDOG|sigmask);
  373.         if(sig&SIG_MAIN_DARK_WATCHDOG){
  374.             p_data_proc->flg_watchdog--;
  375.         }
  376.     }while( (sig&(SIGBREAKF_CTRL_C|sigmask))==0);
  377.     if(sig&SIGBREAKF_CTRL_C){
  378.         flg_ctrl_c=TRUE;
  379.     }
  380.     return(sig&sigmask);
  381. }
  382.  
  383. /**********************************************************/
  384. /* This function only return, when superdark have to exit */
  385. /**********************************************************/
  386.  
  387. void    wait_end(void)
  388. {
  389.     SDwait(0);
  390. }
  391.